package com.cqxyd.smartsite.service.impl;

import com.cqxyd.smartsite.dao.UploadRecordMapper;
import com.cqxyd.smartsite.pojo.UploadRecord;
import com.cqxyd.smartsite.pojo.UploadRecordVo;
import com.cqxyd.smartsite.service.UploadRecordService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Angus Yu
 * @date 2020-05-14 11:07
 */
@Service
public class UploadRecordServiceImpl implements UploadRecordService {
    @Autowired
    private UploadRecordMapper uploadRecordMapper;

    @Override
    public int insertAndGetId(String serialNo, Date uploadTime, String uploadInfo, String uploadType) {
        UploadRecord record = new UploadRecord();
        record.setSerialNo(serialNo);
        record.setUploadTime(uploadTime);
        record.setUploadInfo(uploadInfo);
        record.setUploadType(uploadType);
        int affectRowCount = uploadRecordMapper.insertAndGetId(record);
        return affectRowCount == 1 ? record.getUploadRecordId() : affectRowCount;
    }

    @Override
    public int insert(String serialNo, Date uploadTime, String uploadInfo, String uploadType) {
        UploadRecord record = new UploadRecord();
        record.setSerialNo(serialNo);
        record.setUploadTime(uploadTime);
        record.setUploadInfo(uploadInfo);
        record.setUploadType(uploadType);
        return uploadRecordMapper.insert(record);
    }

    @Override
    public PageInfo<UploadRecordVo> getUploadRecordListByPage(Integer pageNum, Integer pageSize, String serialNo, String type, String timeStart, String timeEnd) {
        List<UploadRecordVo> infoList = uploadRecordMapper.selectUploadRecord(serialNo, type, timeStart, timeEnd);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (UploadRecordVo info : infoList) {
            info.setUploadTimeStr(sdf.format(info.getUploadTime()));
        }
        PageInfo<UploadRecordVo> pageInfo = new PageInfo<UploadRecordVo>(infoList);
        return pageInfo;
    }
}
